package com.ucomm.talent.activity.detail;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewStub;
import android.widget.TextView;

import com.ucomm.talent.AppConstants;
import com.ucomm.talent.AppException;
import com.ucomm.talent.R;
import com.ucomm.talent.activity.IHeaderBarClickListener;
import com.ucomm.talent.common.DebugUtil;
import com.ucomm.talent.manager.DetailSearchManager;
import com.ucomm.talent.model.NewsDetailsInfo;
import com.ucomm.talent.model.NewsInfo;
import com.ucomm.talent.widget.CustomProgressDialog;
import com.ucomm.talent.widget.MainHeaderBar;

public class NewsDetailsActivity extends Activity implements IHeaderBarClickListener{
	
	private MainHeaderBar mainHeaderBar;
	private int id = 0;
	private CustomProgressDialog customProgressDialog;
	private NewsInfo info;
	private TextView title;
	private ViewStub news_details_vs1;
	private ViewStub news_details_vs2;
	private TextView author_tv;
	private TextView datetime_tv;
	private TextView keyword_tv;
	private TextView news_description;
	
	Handler mHandler = new Handler(){
        @Override
    public void handleMessage(Message msg) {
        int what = msg.what;
        DebugUtil.Log(NewsDetailsActivity.this,"---------- " + customProgressDialog.toString());
        switch (what){
            case AppConstants.MsgStatus.MSG_ERROR:
                if (customProgressDialog != null && customProgressDialog.isShowing())
                    customProgressDialog.dismiss();
                ((AppException)msg.obj).makeToast(NewsDetailsActivity.this);
                break;
            case AppConstants.MsgStatus.MSG_SUCCUSS:
                if (customProgressDialog != null && customProgressDialog.isShowing())
                    customProgressDialog.dismiss();
                //获取数据
                NewsDetailsInfo details = (NewsDetailsInfo) msg.obj;
                title.setText(details.getTitle());
                author_tv.setText(details.getAuthor());
                datetime_tv.setText(details.getDatetime()+"");
                keyword_tv.setText(details.getKeyword());
                news_description.setText(details.getDescription()+"");
                break;
            case AppConstants.MsgStatus.MSG_LOADING:
                if (customProgressDialog != null && !customProgressDialog.isShowing())
                    customProgressDialog.show();
                break;
            default:
                break;
        }
        super.handleMessage(msg);
    }
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.news_details_activity);
		Intent i = getIntent();
		info = (NewsInfo) i.getSerializableExtra("info");
		id = info.getNewsId();
		
		initView();
		DetailSearchManager detailSearchManager = new DetailSearchManager(this, mHandler);
        final String url = AppConstants.NEWS_DETAILS+id;
        detailSearchManager.searchDetailDataFromNetwork(url);
	}
	private void initView(){
		mainHeaderBar = (MainHeaderBar) findViewById(R.id.main_header_bar);
		mainHeaderBar.setBarClickListener(this);
		customProgressDialog = new CustomProgressDialog(this);
		title = (TextView) findViewById(R.id.title);
		news_details_vs1 = (ViewStub) findViewById(R.id.news_details_vs1);
		news_details_vs2 = (ViewStub) findViewById(R.id.news_details_vs2);
		news_details_vs1.inflate();
		news_details_vs2.inflate();
		author_tv = (TextView) findViewById(R.id.author_tv);
		datetime_tv = (TextView) findViewById(R.id.datetime_tv);
		keyword_tv = (TextView) findViewById(R.id.keyword_tv);
		news_description = (TextView) findViewById(R.id.news_description);
		
	}
	@Override
	public void onBackButtOnClick(View view) {
		// TODO Auto-generated method stub
		finish();
	}

	@Override
	public void onExitButtonOnClick(View view) {
		// TODO Auto-generated method stub
		
	}

}
